<?php
namespace App\Admin\Controllers\Traits\WxPetTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Models\WxIcon;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formPetsTrait{
    public function index_config(Content $content)
    {
        return $content->title('宠物系统配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_config()->render()}</div>
HTML
            );
    }
    protected function form_config(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('领养首页', function (Form $form) {
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">一、审核相关</div>';
            }, '');

            $form->radio('is_audit_pets_adoption', '发布领养是否需要审核')->help('AI审核需要配置百度文字审核API，<a href="'.admin_url('settings/api').'" target="_blank">点击进入</a>')->options($this->audit_options)->default(Settings::get('is_audit_pets_adoption', 0))->when('!=', 1, function (Form $form){
                $form->checkbox('audit_pets_adoption_white_list', '发布领养审核白名单')->options(FieldUtils::userPermissions())->default(Settings::get('audit_pets_adoption_white_list', ''));
            });

            $form->divider();

            $form->radio('is_audit_pets_comment', '留言是否需要审核')->help('AI审核需要配置百度文字审核API，<a href="'.admin_url('settings/api').'" target="_blank">点击进入</a>')->options($this->audit_options)->default(Settings::get('is_audit_pets_comment', 0))->when('!=', 1, function (Form $form){
                $form->checkbox('audit_pets_comment_white_list', '留言审核白名单')->options(FieldUtils::userPermissions())->default(Settings::get('audit_pets_comment_white_list', ''));
            });
            $form->divider();
            $form->html(function (){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">二、首页</div>';
            }, '');


            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">注意：需要先配置高得web服务key，<a href="'.admin_url('settings/api').'" target="_blank">点击跳转</a></div>';
            }, '');

            $form->multipleSelectTable('pets_adoption_index_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formPetsTrait:pets_adoption_index_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')->default(Settings::get('pets_adoption_index_banner', ''));

//            $form->table('pets_adoption_index_notice','垂直滚动通知', function (NestedForm $table) {
//                $table->text('text', '文本')->default('文字');
//                $table->image('img', '左侧图')->help('<a href="https://img.mini.minisns.cn/2023/06/23/c8186968da891a2dac10ff5d4fbe0351.png" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->default('https://img.mini.minisns.cn/2023/06/23/c8186968da891a2dac10ff5d4fbe0351.png')->uniqueName()->url('files/uploads')->autoUpload();
//                $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
//                $table->text('target', '跳转id')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
//            })->default(Settings::get('pets_adoption_index_notice', ''));
            $form->multipleSelectTable('pets_adoption_index_nav', '格子菜单')
                ->title('图标库')
                ->max(10)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formPetsTrait:pets_adoption_index_nav')
                ->help('领养首页的格子菜单， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('pets_adoption_index_nav', ''));

        });

        $form->tab('发布', function (Form $form) {
            $form->radio('pets_adoption_add_force_bind_phone', '强制绑定手机号')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('pets_adoption_add_force_bind_phone', 0, true));
            $form->radio('pets_adoption_add_force_real_name', '强制实名')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('pets_adoption_add_force_real_name', 0, true));
        });

        $form->tab('详情页', function (Form $form) {
            $form->multipleSelectTable('pets_adoption_detail_user_info_img', '关注公众号')
                ->title('图标库')
                ->max(1)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formPetsTrait:pets_adoption_detail_user_info_img')
                ->help('在用户信息的下边， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('pets_adoption_detail_user_info_img', ''));
        });

        $form->action('pets/action/config');
        return $form;
    }

    public function config(Request $request){
        SiteUtils::forget_config_data();


        $audit_pets_adoption_white_list = $request->input('audit_pets_adoption_white_list');
        $audit_pets_adoption_white_list_ = [];
        if($audit_pets_adoption_white_list){
            foreach ($audit_pets_adoption_white_list as $i){
                if($i === null){
                    continue;
                }
                $audit_pets_adoption_white_list_[] = (int)$i;
            }
        }

        Settings::set('audit_pets_adoption_white_list', $audit_pets_adoption_white_list_, true);


        $audit_pets_comment_white_list = $request->input('audit_pets_comment_white_list');
        $audit_pets_comment_white_list_ = [];
        if($audit_pets_comment_white_list){
            foreach ($audit_pets_comment_white_list as $i){
                if($i === null){
                    continue;
                }
                $audit_pets_comment_white_list_[] = (int)$i;
            }
        }

        Settings::set('audit_pets_comment_white_list', $audit_pets_comment_white_list_, true);

        Settings::set('is_audit_pets_adoption', $request->input('is_audit_pets_adoption'), true);
        Settings::set('is_audit_pets_comment', $request->input('is_audit_pets_comment'), true);

        Settings::set('pets_adoption_index_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formPetsTrait:pets_adoption_index_banner', $request->input('pets_adoption_index_banner')), true);
//        Settings::set('pets_adoption_index_notice', AdminUtils::_table_data($request->input('pets_adoption_index_notice')), true);
        Settings::set('pets_adoption_index_nav', AdminUtils::_multipleSelectTable_order_process('selectTable:formPetsTrait:pets_adoption_index_nav', $request->input('pets_adoption_index_nav')), true);
        Settings::set('pets_adoption_detail_user_info_img', AdminUtils::_multipleSelectTable_order_process('selectTable:formPetsTrait:pets_adoption_detail_user_info_img', $request->input('pets_adoption_detail_user_info_img')), true);

        Settings::set('pets_adoption_add_force_bind_phone', $request->input('pets_adoption_add_force_bind_phone'), true, true);
        Settings::set('pets_adoption_add_force_real_name', $request->input('pets_adoption_add_force_real_name'), true, true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
